//
//  ViewController.m
//  CoreData082568
//
//  Created by 王婷 on 15/11/20.
//  Copyright © 2015年 王婷. All rights reserved.
//

#import "ViewController.h"
#import "CoreDataManager.h"
#import "Student.h"

@interface ViewController ()
- (IBAction)add:(id)sender;

- (IBAction)select:(id)sender;
- (IBAction)update:(id)sender;
- (IBAction)delete:(id)sender;

@property (nonatomic, strong)CoreDataManager *manager;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    // 创建一个数据管理器
    self.manager = [CoreDataManager defaults];// 单例方法初始化
    NSLog(@"url ======= %@", [self.manager applicationDocumentsDirectory]);
    [self.manager applicationDocumentsDirectory];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -- coreData添加方法
- (IBAction)add:(id)sender {
    
    // 创建一个实体描述类
    // 参数1: 描述的实体
    // 参数2: 数据管理器
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.manager.managedObjectContext];
    
    
    // 创建一个Student实体
    // 参数1: 实体描述
    // 参数2: 数据管理器
    
    Student *stu = [[Student alloc]initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
    stu.name = @"耍耍";
    stu.age = @22;// 这里的NSInteger全会被转化成NSNumber
    stu.sex = @"不确定";
    stu.number = @12;
    stu.hobby = @"睡觉😔";
    [self.manager saveContext];
}

// 查找
- (IBAction)select:(id)sender {
    // 获取查询数据的请求, 相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"Student"];// 实体
    // 生成约束 谓词(只有两种答案)
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"],  @"耍耍"];
    request.predicate = predicate;// 给查询请求添加约束(谓词)
    // 通过某个key 给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    NSLog(@"%ld", array.count);
//    NSLog(@"%@", array);
}

// 修改
- (IBAction)update:(id)sender {
    
    // 获取查询数据的请求, 相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"Student"];// 实体
    // 生成约束 谓词(只有两种答案)
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"],  @"耍耍"];
    request.predicate = predicate;// 给查询请求添加约束(谓词)
    // 通过某个key 给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    Student *stu = [array lastObject];
    stu.sex = @"可能是一个女的吧";
    [self.manager saveContext];
    
}

// 删除
- (IBAction)delete:(id)sender {
    // 获取查询数据的请求, 相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"Student"];// 实体
    // 生成约束 谓词(只有两种答案)
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"sex", @"%@"], @"不确定"];
    request.predicate = predicate; // 给查询请求添加约束(谓词)
    // 通过某个key 给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
    for (Student *stu in array) {
        [self.manager.managedObjectContext deleteObject:stu];
    }
    
    [self.manager saveContext];

}
@end
